package com.ruoyi.factory.epros.common;

import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName SendUtils
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2023/2/10 8:47
 **/
public class SendUtils {


    public  static String okHttpPost(String url,String json) {
        OkHttpClient okHttpClient = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType,json);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            return Objects.requireNonNull(response.body()).string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public  static String okHttpGet(String url, Map<String,String> param) {
        //拼接参数
        url = url + "?";
        for(String key:param.keySet()){
            url = url + key + "=" + param.get(key) + "&";
        }
        //发送请求
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build(); //添加头部信息
        try {
            Response response = okHttpClient.newCall(request).execute();
            return Objects.requireNonNull(response.body()).string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
